//
//  AIGirlProfileTableViewCell.swift
//  AIGirl
//
//  Created by Rae on 2023/12/28.
//

import UIKit

class AIGirlProfileTableViewCell: UITableViewCell, AnimationProtocol, UorInXNetworkProtocol {

    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(AIGirlProfileItem.self)
        tableView.rowHeight = 60
        tableView.showsVerticalScrollIndicator = false
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        return tableView
    }()
    
    var itemInfos: [AIGirlProfileModel] = [AIGirlProfileModel]() {
        didSet{
            tableView.reloadData()
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            tableView
        }
        
        tableView.backgroundColor = "#FBFBFB".toColor.alpha(0.06)
        tableView.layer.cornerRadius = 21
        tableView.layer.masksToBounds = true
        tableView.leading(15).trailing(15).top(0).bottom(0)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

extension AIGirlProfileTableViewCell: UITableViewDelegate, UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        itemInfos.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.cell(AIGirlProfileItem.self, indexPath: indexPath)
        cell.model = itemInfos[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let model = itemInfos[indexPath.row]
        if model == .account {
            UIApplication.topController()?.navigationController?.pushViewController(AIGirlAccountViewController(), animated: true)
        }else if model == .clear {
            let clearView = AIGirlClearView()
            clearView.clearClosure = {
                self.request.start(.clearMessage) { result in
                    if result.isSuccessful {
                        NotificationCenter.default.post(name: .updateChatHistory, object: nil)
                        "Success!".toast_success
                    }
                }
            }
            animation.animation(show: clearView)
        }else if model == .get {
            UIApplication.topController()?.navigationController?.pushViewController(AIGirlProfileGetViewController(), animated: true)
        }else if model == .like {
            UIApplication.topController()?.navigationController?.pushViewController(AIGirlLikesViewController(), animated: true)
        } else if model == .term || model == .privacy {
            let controller = AIGirlWebViewController()
            controller.titleName = model.title
            controller.urlString = model == .term ? AGirlConstants.terms : AGirlConstants.policy
            UIApplication.topController()?.navigationController?.pushViewController(controller, animated: true)
        }
    }
    
}

class AIGirlProfileItem: UITableViewCell {

    private let iconImageView = c_make_ex(UIImageView.self).end()
    private let titleLabel = c_make_ex(UILabel.self)
        .font(17.font_medium).color(.white).end()
    private let rightImageView = c_make_ex(UIImageView.self)
        .image("AIGirl_Profile_right").end()
    
    var model: AIGirlProfileModel = .none {
        didSet{
            iconImageView.image = model.icon.toImage
            titleLabel.text = model.title
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            iconImageView
            titleLabel
            rightImageView
        }
        
        iconImageView.leading(10).size(20).centerVertically()
        titleLabel.leading(40).centerVertically()
        rightImageView.trailing(15).width(12).height(22).centerVertically()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

